﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNHang.BUS;
using QLNHang.DTO;

namespace QLNHang.GUI
{
    public partial class ucThemKhachHang : UserControl
    {
        public ucThemKhachHang()
        {
            InitializeComponent();
            btnSuaKhachHang.Enabled = false;
            GridLoad();
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text.Length > 0)
            {
            KhachHangDTO kh = new KhachHangDTO();
            kh.HoTenKhachHang = txtHoTen.Text.Trim();
            kh.DiaChi = txtDiaChi.Text.Trim();
            kh.SoDienThoai = txtSoDienThoai.Text.Trim();
            kh.SoCMND = txtCMND.Text.Trim();
            try
            {
                new KhachHangBUS().ThemKhachHang(kh);
                MessageBox.Show("Thêm khách hàng "+ kh.HoTenKhachHang +" thành công");
                resetControl();
                GridLoad();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            }
            else
            {
                MessageBox.Show("Nhập tên khách hàng");
            }
        }
        private void resetControl()
        {
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtSoDienThoai.Text = "";
            txtCMND.Text = "";
            lbMaKH.Text = "";
        }


       
        private void GridLoad()
        {
            KhachHangDTO kh = new KhachHangDTO();
            gvKhachHang.DataSource = new KhachHangBUS().XemKhachHang(kh);
            
        }


        private void gvKhachHang_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            gvKhachHang.ClearSelection();
            gvKhachHang.CurrentCell = null;
            txtHoTen.Focus();
        }

        private void gvKhachHang_MouseClick(object sender, MouseEventArgs e)
        {
            var selectedRow = gvKhachHang.SelectedRows[0];
            btnThemKH.Enabled = false;
            if (selectedRow != null)
            {
                btnSuaKhachHang.Enabled = true;
                txtHoTen.Text = selectedRow.Cells["TenKH"].Value.ToString();
                txtDiaChi.Text = selectedRow.Cells["DiaChi"].Value.ToString();
                txtSoDienThoai.Text = selectedRow.Cells["SoDienThoai"].Value.ToString();
                txtCMND.Text = selectedRow.Cells["CMND"].Value.ToString();
                lbMaKH.Text = selectedRow.Cells["MaKH"].Value.ToString();
            }
            else
            {
                btnSuaKhachHang.Enabled = false;
            }
        }

        private void btnSuaKhachHang_Click(object sender, EventArgs e)
        {

            KhachHangDTO kh = new KhachHangDTO();
            kh.MaKhachHang = lbMaKH.Text.Trim();
            kh.HoTenKhachHang = txtHoTen.Text.Trim();
            kh.DiaChi = txtDiaChi.Text.Trim();
            kh.SoDienThoai = txtSoDienThoai.Text.Trim();
            kh.SoCMND = txtCMND.Text.Trim();
            try
            {
                new KhachHangBUS().SuaKhachHang(kh);
                MessageBox.Show("Sửa thông tin khách hàng " + kh.HoTenKhachHang + " thành công");
                GridLoad();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            btnSuaKhachHang.Enabled = false;
            gvKhachHang.ClearSelection();
            gvKhachHang.CurrentCell = null;
            txtHoTen.Focus();
            resetControl();
        }

       
    }
}
